package main

import (
	"time"
	"math/rand"
	"match-spider/util/prop"
	"github.com/gocolly/colly"
	"fmt"
	"github.com/json-iterator/go"
	"match-spider/util/pg"
	"strings"
)
func fetchNews(date string)  {
	url:="https://query-api.8win.com/command/execute?command=200017&matchDate="+date

	c := colly.NewCollector()
	c.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"

	c.OnResponse(func(response *colly.Response) {

		m:=make(map[string]interface{})
		json := jsoniter.ConfigCompatibleWithStandardLibrary
		json.Unmarshal(response.Body,&m)

		if m["data"]==nil{
			fmt.Println("Error=====>",date,"========",m)
			return
		}
		data:=m["data"].(map[string]interface{})
		list:=data["data"]
		if list==nil{
			return
		}
		storeNews(date,list.([]interface{}))
	})
	c.OnRequest(func(r *colly.Request) {
		fmt.Println("Visiting", r.URL.String())
	})
	c.OnError(func(r *colly.Response, err error) {
		fmt.Println("Error Request URL:", r.Request.URL)

	})

	c.Visit(url)
}
func pgsqlRealEscapeString(value string) string {
	replace := map[string]string{"\\":"\\\\", "'":`''`, "\\0":"\\\\0", "\n":"\\n", "\r":"\\r", `"`:`\"`, "\x1a":"\\Z"}

	for b, a := range replace {
		value = strings.Replace(value, b, a, -1)
	}

	return value
}
func storeNews(date string,list []interface{}){
	k:=0
	sql:="insert into t_match_news(id,match_key,match_date,title,sub_title,content,publish_time,tips_type_id," +
		"tips_type_name,pic_url,team_type_id,with_pic,created_at) values"

	createdAt:=time.Now().Format("2006-01-02 15:04:05")
	for _,v:=range list {
		match:=v.(map[string]interface{})
		matchKey:=match["matchId"].(string)
		tips:=match["tips"].([]interface{})
		for _, v1 := range tips {
			t := v1.(map[string]interface{})
			id := t["id"].(string)
			content:=t["content"].(string)
			content=pgsqlRealEscapeString(content)
			sql += "("
			sql += "'" + id + "',"
			sql += "'" + matchKey + "',"
			sql += "'" + date + "',"
			sql += "'" + t["title"].(string) + "',"
			sql += "'" + t["subTitle"].(string) + "',"
			sql += "'" + content + "',"
			sql += "'" + t["publishTime"].(string) + "',"
			sql += "'" + t["tipsTypeId"].(string) + "',"
			sql += "'" + t["tipsTypeName"].(string) + "',"
			sql += "'" + t["picUrl"].(string) + "',"
			sql += "'" + t["teamTypeId"].(string) + "',"
			sql += "'" + t["withPic"].(string) + "',"
			sql += "'" + createdAt + "'"
			sql += "),"
			k++
		}
	}
	if k > 0 {
		sql = sql[0 : len(sql)-1]
		pg.Insert(sql)
	}
}
func main()  {

	startAt:=prop.Get("match","date.start")
	endAt:=prop.Get("match","date.end")
	startTime,_:=time.Parse("2006-01-02",startAt)
	endTime,_:=time.Parse("2006-01-02",endAt)

	for{
		if(endTime.Sub(startTime).Hours()<0){
			break
		}

		nextTimeString:=time.Unix(startTime.Unix(),0).Format("2006-01-02")
		fetchNews(nextTimeString)
		seed:=500+rand.Intn(1000)
		time.Sleep(time.Millisecond*time.Duration(seed))
		startTime=startTime.Add(time.Hour*24)
	}
}
